********************************************************************************
* Data preparation: Demographics
********************************************************************************

* Children
gen child = 1 if age <= 18
replace child = 0 if age > 18 & age != .
label var child "Age 18 or younger"
bysort serial: egen nkids = total(child), missing
*replace nkids = 3 if nkids >= 3 & nkids != .
label var nkids "Number of individuals age 18 or younger in HH"

* Young children
gen ychild = 1 if age <= 12
replace ychild = 0 if age > 12 & age != .
label var ychild "Age 12 or younger"
bysort serial: egen nykids = total(ychild), missing
*replace nykids = 3 if nykids >= 3 & nykids != .
label var nykids "Number of individuals age 12 or younger in HH"

* Between 19 and 24 year olds
gen yadult = 1 if age >= 19 & age <= 24
replace yadult = 0 if (age < 19 | age > 24) & age != .
label var yadult "Age between 19 and 24"
bysort serial: egen no1924 = total(yadult), missing
*replace no1924 = 3 if no1924 >= 3 & no1924 != .
label var no1924 "Number of individuals between 19 and 24 in HH"

* Under 25 year olds
gen u25 = 1 if age < 25
replace u25 = 0 if age >= 25 & age != .
label var u25 "Younger than 25"
bysort serial: egen nu25 = total(u25), missing
*replace nu25 = 3 if nu25 >= 3 & nu25 != .
label var nu25 "Number of individuals younger than 25 in HH"

* Over 60 year olds
gen o60 = 1 if age > 60
replace o60 = 0 if age <= 60 & age != .
label var o60 "Older than 60"
bysort serial: egen no60 = total(o60), missing
*replace no60 = 3 if no60 >= 3 & no60 != .
label var no60 "Number of individuals older than 60 in HH"

* Over 70 year olds
gen o70 = 1 if age > 70
replace o70 = 0 if age <= 70 & age != .
label var o70 "Older than 70"
bysort serial: egen no70 = total(o70), missing
*replace no70 = 3 if no70 >= 3 & no70 != .
label var no70 "Number of individuals older than 70 in HH"

* Between 61 and 70 year olds
gen oadult = 1 if age >= 61 & age <= 70
replace oadult = 0 if (age < 61 | age > 70) & age != .
label var oadult "Age between 61 and 70"
bysort serial: egen no6170 = total(oadult), missing
*replace no6170 = 3 if no6170 >= 3 & no6170 != .
label var no6170 "Number of individuals between 61 and 70 in HH"

* Between 25 and 60 year olds
gen adult = 1 if age >= 25 & age <= 60
replace adult = 0 if (age < 25 | age > 60) & age != .
label var adult "Age between 25 and 60"
bysort serial: egen no2560 = total(adult), missing
*replace no2560 = 3 if no2560 >= 3 & no2560 != .
label var no2560 "Number of individuals between 25 and 60 in HH"

* Education
gen educ4 = .
replace educ4 = 1 if educ <= 72					// less than high school degree
replace educ4 = 2 if educ == 73					// high school degree
replace educ4 = 3 if educ > 73 & educ <= 100	// less than bachelor's degree
replace educ4 = 4 if educ >= 110 & educ != .	// at least college education
label var educ4 "Four education categories"

gen college = 1 if educ4 == 4
replace college = 0 if educ4 < 4
label var college "Has college degree"

* Race
gen white = 1 if race == 100
replace white = 0 if race != 100 & race != .
label var white "Race is white"

* Sex
gen female = sex - 1
label var female "Female"

* Variables for other household members: based on family relationships
gen serial_id = 1 if relate == 101							// household head is person 1
replace serial_id = 2 if relate == 201 | relate == 1114		// spouse or unmarried partner is person 2
*duplicates report serial serial_id if serial_id != .

* Store spouse data with head observation
*count if pecohab != 0 & aspouse != 0
gen sploc_gen = pecohab + aspouse
rangestat (mean) age_sp = age, by(serial) interval(lineno sploc_gen sploc_gen)
rangestat (mean) white_sp = white, by(serial) interval(lineno sploc_gen sploc_gen)
rangestat (mean) college_sp = college, by(serial) interval(lineno sploc_gen sploc_gen)
rangestat (mean) female_sp = female, by(serial) interval(lineno sploc_gen sploc_gen)
rangestat (mean) educ4_sp = educ4, by(serial) interval(lineno sploc_gen sploc_gen)
